<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__PUBLIC__/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加编辑</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group" id="myform" action="javascript:;">
                <div class="layui-form-item">
                    <label class="layui-form-label">广告名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="ae_name" autocomplete="off" value="{$find.ae_name}" placeholder="请输入广告名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">广告图片</label>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <input type="hidden" name="headImg" id="headImg">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" src="{$find.ae_img}" id="demo1" width="200" style="padding-left: 100px;">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">广告链接</label>
                    <div class="layui-input-block">
                        <input type="text" name="ae_href" autocomplete="off" value="{$find.ae_href}" placeholder="请输入广告链接" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否显示</label>
                    <div class="layui-input-block">
                        <input type="radio" name="ae_top" value="1" title="显示" {if $find.ae_top == 1} checked {/if}>
                        <input type="radio" name="ae_top" value="0" title="隐藏" {if $find.ae_top == 0} checked {/if}>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="ae_sort" autocomplete="off" value="{$find.ae_sort}" placeholder="请不要乱输入哦" class="layui-input">
                    </div>
                </div>
                <input type="hidden" name="ae_id" value="{$find.ae_id}">
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" lay-filter="component-form-demo1" onclick="sub()">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="__PUBLIC__/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['form', 'layer'], function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate;

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });

        /* 自定义验证规则 */
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
                if(value.length > 100){
                    return '标题字数不得超出100';
                }
            }
        });
    });
    function sub(){
        $.ajax({
            type:"POST",
            url:'{:url("Adver/shuffAddEdit")}',
            data:$("#myform").serialize(),
            success:function(data){
                if(data.code>0){
                    layer.msg(data.msg,{icon:1,time:1500});
                    setTimeout(function(){parent.location.reload()},1500);
                }else{
                    layer.msg(data.msg,{icon:5,time:1500});
                }
            }
        });
    }
    //封面上传
    layui.use('upload', function() {
        var $ = layui.jquery
            , upload = layui.upload;

        var id = $("[name=id]").val();
        var url = '{:url("Adver/uploads")}';
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: url
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if(res.code < 0){
                    return layer.msg(res.msg);
                }else{
                    //上传成功
                    layer.msg(res.msg);
                    $("[name=headImg]").val(res.src);
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>
</html>
